package com.lmk.dsa.struct.commons;

import java.util.Objects;

/**
 * 图的边
 * @author LaoMake
 * @since 1.0
 */
public class Edge implements Comparable {

    /** 起始索引 */
    public int from;

    /** 目标索引 */
    public int to;

    /** 权值 */
    public int weight;

    public Edge(int from, int to) {
        this(from, to, 1);
    }

    public Edge(int from, int to, int weight) {
        this.from = from;
        this.to = to;
        this.weight = weight;
    }

    /**
     * 获取另外一个定点索引
     * @param v
     * @return
     */
    public int other(int v){
        assert(v == from || v == to);
        return v == from ? to : from;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Edge edge = (Edge) o;
        return from == edge.from &&
                to == edge.to;
    }

    @Override
    public int hashCode() {
        return Objects.hash(from, to);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{")
          //.append(from).append(", ")
          .append(to)
          .append(", ").append(weight)
          .append("}");

        return sb.toString();
    }

    @Override
    public int compareTo(Object o) {
        Edge edge = (Edge) o;
        return this.weight - edge.weight;
    }
}
